package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.glowmee.cmd.ReviewCommentCMD;
import com.glowdayz.glowmee.dao.BrandDAO;
import com.glowdayz.glowmee.dao.ReviewCommentDAO;

public class ReviewCommentBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public ReviewCommentBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		String where = request.getParameter("where");
		if (where != null) {
			where = Util.getInstance().incoding(where);
		}
		
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		
		if (order != null && order.equals("isUnDisplay")) {
			order = "c.isDisplay ASC, c.create_date DESC";
			request.setAttribute("order", "&order=isUnDisplay");
		} else if (order != null && order.equals("isDisplay")) {
			order = "c.isDisplay DESC, c.create_date DESC";
			request.setAttribute("order", "&order=isDisplay");
		}
		
		ReviewCommentDAO dao = ReviewCommentDAO.getInstance();
		
		String isDisplay_str = request.getParameter("isDisplay");
		String uesrId_str = request.getParameter("uesrId");
		String productId_str = request.getParameter("productId");
		if (uesrId_str != null && productId_str != null && isDisplay_str != null) {
			int uesrId = Integer.parseInt(uesrId_str);
			int productId = Integer.parseInt(productId_str);
			int isDisplay = Integer.parseInt(isDisplay_str);
			
			if (isDisplay == 1) {
				isDisplay = 0;
			} else if (isDisplay == 0) {
				isDisplay = 1;
			}
			
			ReviewCommentCMD cmd = new ReviewCommentCMD(uesrId, productId, isDisplay);
			try {
				dao.unDisplayReviewComment(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			int brandTotal = DBHandler.getInstance().getCount("brand", "idBrand", null);
			request.setAttribute("brand", BrandDAO.getInstance().getBrandList(1, brandTotal, "brandTitle", null));
			if (where != null) request.setAttribute("where", "&where=" + URLEncoder.encode(where, "UTF-8"));
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("reviewComment", dao.getReviewCommentList(page, 10, order, where));
			if (where == null) {
				request.setAttribute("display", DBHandler.getInstance().getCount("reviewcomment c, register r, product p, brand b", "c.create_date",
						"c.isDisplay='1' AND c.idRegister=r.idRegister AND c.idProduct=p.idProduct AND p.idBrand=b.idBrand"));
				request.setAttribute("unDisplay", DBHandler.getInstance().getCount("reviewcomment c, register r, product p, brand b", "c.create_date",
						"c.isDisplay='0' AND c.idRegister=r.idRegister AND c.idProduct=p.idProduct AND p.idBrand=b.idBrand"));
			} else {
				request.setAttribute("display", DBHandler.getInstance().getCount("reviewcomment c, register r, product p, brand b", "c.create_date",
						"c.isDisplay='1' AND c.idRegister=r.idRegister AND c.idProduct=p.idProduct AND p.idBrand=b.idBrand" + where));
				request.setAttribute("unDisplay", DBHandler.getInstance().getCount("reviewcomment c, register r, product p, brand b", "c.create_date",
						"c.isDisplay='0' AND c.idRegister=r.idRegister AND c.idProduct=p.idProduct AND p.idBrand=b.idBrand" + where));
			}
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/product/reviewComment.jsp");
		dpr.forward(request, response);
	}

	/** 검색결과로 처리되는 구간 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
    	request.setCharacterEncoding("utf-8");
		
		String where = "";
		
		String nickName = request.getParameter("nickName");
		String created_date1 = request.getParameter("created_date1");
		String created_date2 = request.getParameter("created_date2");
		String idBrand = request.getParameter("brand");
		String productTitle = request.getParameter("product");
		
		if (nickName != null && !nickName.equals("")) {
			where += " AND r.nickName like '%" + nickName + "%'";
		}
		if (!created_date1.equals("") && !created_date2.equals("")) {
			created_date1 = created_date1.replace("-", "");
			created_date1 += "000000";
			created_date2 += "999999";
			created_date2 = created_date2.replace("-", "");
			where += " AND c.create_date BETWEEN " + created_date1 + " AND " + created_date2;
		}
		if (idBrand != null && !idBrand.equals("--- 선택하세요 ---")) {
			where += " AND p.idBrand = " + idBrand;
		}
		if (productTitle != null && !productTitle.equals("")) {
			where += " AND p.productTitle like '%" + productTitle + "%'";
		}
		
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		
		if (order != null && order.equals("isUnDisplay")) {
			order = "c.isDisplay ASC, c.create_date DESC";
			request.setAttribute("order", "&order=isUnDisplay");
		} else if (order != null && order.equals("isDisplay")) {
			order = "c.isDisplay DESC, c.create_date DESC";
			request.setAttribute("order", "&order=isDisplay");
		}
		
		ReviewCommentDAO dao = ReviewCommentDAO.getInstance();
		
		String isDisplay_str = request.getParameter("isDisplay");
		String uesrId_str = request.getParameter("uesrId");
		String productId_str = request.getParameter("productId");
		if (uesrId_str != null && productId_str != null && isDisplay_str != null) {
			int uesrId = Integer.parseInt(uesrId_str);
			int productId = Integer.parseInt(productId_str);
			int isDisplay = Integer.parseInt(isDisplay_str);
			
			if (isDisplay == 1) {
				isDisplay = 0;
			} else if (isDisplay == 0) {
				isDisplay = 1;
			}
			
			ReviewCommentCMD cmd = new ReviewCommentCMD(uesrId, productId, isDisplay);
			try {
				dao.unDisplayReviewComment(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			int brandTotal = DBHandler.getInstance().getCount("brand", "idBrand", null);
			request.setAttribute("brand", BrandDAO.getInstance().getBrandList(1, brandTotal, "brandTitle", null));
			if (where != null) request.setAttribute("where", "&where=" + URLEncoder.encode(where, "UTF-8"));
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("reviewComment", dao.getReviewCommentList(page, 10, order, where));
			request.setAttribute("display", DBHandler.getInstance().getCount("reviewcomment c, register r, product p, brand b", "c.create_date",
					"c.isDisplay='1' AND c.idRegister=r.idRegister AND c.idProduct=p.idProduct AND p.idBrand=b.idBrand" + where));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("reviewcomment c, register r, product p, brand b", "c.create_date",
					"c.isDisplay='0' AND c.idRegister=r.idRegister AND c.idProduct=p.idProduct AND p.idBrand=b.idBrand" + where));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/product/reviewComment.jsp");
		dpr.forward(request, response);
		
	}

}